package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.pojo.system.Resource;
import com.qingcheng.service.system.AdminService;
import com.qingcheng.service.system.ResourceService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDetailServiceImpl implements UserDetailsService {


    @Reference
    private AdminService adminService;

    @Reference
    private ResourceService resourceService;

    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("经过了UserDetailServiceImpl");

        Map map = new HashMap<>();
        map.put("loginName", s);
        map.put("status", "1");
        List<Admin> list = adminService.findList(map);
        if (list.size() == 0) {
            return null;
        }

        //实际项目中应该从数据库中提取用户的角色列表
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        //得到该用户的权限列表
        List<String> resources = resourceService.findResourcesByLoginName(s);
        //遍历添加权限
        if (resources != null) {
            System.out.println(resources.size());
            for (String resource : resources) {
                if(resource!=null){
                    grantedAuthorities.add(new SimpleGrantedAuthority(resource));
                }
            }
        }
        return new User(s, list.get(0).getPassword(), grantedAuthorities);
    }
}
